package com.frank.practice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.frank.common.entity.PageInfo;
import com.frank.common.entity.Result;
import com.frank.practice.api.req.GetPracticeSubjectListReq;
import com.frank.practice.api.req.GetPracticeSubjectReq;
import com.frank.practice.api.req.GetPracticeSubjectsReq;
import com.frank.practice.api.req.GetPreSetContentReq;
import com.frank.practice.api.vo.*;
import com.frank.practice.entity.dto.PracticeSetDto;
import com.frank.practice.entity.dto.PracticeSubjectDto;
import com.frank.practice.service.PracticeSetService;
import com.google.common.base.Preconditions;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author Frank Zou
 * @version v0.0.1
 * @date 2025/3/17
 * @description 刷题模块
 */
@RestController
@RequestMapping("/practice/set")
public class PracticeSetController {

    private final PracticeSetService practiceSetService;

    public PracticeSetController(PracticeSetService practiceSetService) {
        this.practiceSetService = practiceSetService;
    }

    @GetMapping("/getSpecialPracticeContent")
    public Result<List<SpecialPracticeVo>> getSpecialPracticeContent() {
        return Result.ok(practiceSetService.getSpecialPracticeContent());
    }


    @PostMapping("/addPractice")
    public Result<PracticeSetVo> addPractice(@RequestBody GetPracticeSubjectListReq req) {
        Preconditions.checkArgument(Objects.nonNull(req), "请求参数不能为空");
        Preconditions.checkArgument(!CollectionUtils.isEmpty(req.getAssembleIds()), "至少需要提供一个标签 ID。");
        PracticeSubjectDto practiceSubjectDto = new PracticeSubjectDto();
        practiceSubjectDto.setAssembleIds(req.getAssembleIds());
        PracticeSetVo practiceSetVo = practiceSetService.addPractice(practiceSubjectDto);
        return Result.ok(practiceSetVo);
    }

    @GetMapping("/getSubjects")
    public Result<PracticeSubjectListVo> getSubjects(GetPracticeSubjectsReq req) {
        Preconditions.checkArgument(Objects.nonNull(req), "请求参数不能为空");
        Preconditions.checkArgument(Objects.nonNull(req.getSetId()), "练习 ID 不能为空");
        PracticeSubjectListVo list = practiceSetService.getSubjects(req);
        return Result.ok(list);
    }

    @GetMapping("/getPracticeSubject")
    public Result<PracticeSubjectVo> getPracticeSubject(GetPracticeSubjectReq req) {
        Preconditions.checkArgument(Objects.nonNull(req), "请求参数不能为空");
        Preconditions.checkArgument(Objects.nonNull(req.getSubjectId()), "题目 ID 不能为空");
        Preconditions.checkArgument(Objects.nonNull(req.getSubjectType()), "题目类型不能为空");
        PracticeSubjectDto practiceSubjectDto = new PracticeSubjectDto();
        practiceSubjectDto.setSubjectId(req.getSubjectId());
        practiceSubjectDto.setSubjectType(req.getSubjectType());
        PracticeSubjectVo practiceSubjectVo = practiceSetService.getPracticeSubject(practiceSubjectDto);
        return Result.ok(practiceSubjectVo);
    }

    /**
     * 获取模拟套卷内容
     *
     * @param req 请求参数
     * @return 模拟套卷内容
     */
    @PostMapping("/getPreSetContent")
    public Result<Page<PracticeSetVo>> getPreSetContent(@RequestBody GetPreSetContentReq req) {
        Preconditions.checkArgument(Objects.nonNull(req), "参数不能为空！");
        PracticeSetDto practiceSetDto = new PracticeSetDto();
        practiceSetDto.setOrderType(req.getOrderType());
        practiceSetDto.setPageInfo(req.getPageInfo());
        practiceSetDto.setSetName(req.getSetName());
        return Result.ok(practiceSetService.getPreSetContent(practiceSetDto));
    }

    /**
     * 获取未完成的练题内容
     * @param pageInfo 分页参数
     * @return 未完成的练题内容
     */
    @GetMapping("/getUnCompletePractice")
    public Result<Page<UnCompletePracticeSetVo>> getUnCompletePractice(PageInfo pageInfo) {
        Preconditions.checkArgument(Objects.nonNull(pageInfo), "参数不能为空！");
        return Result.ok(practiceSetService.getUnCompletePractice(pageInfo));
    }
}
